package xin.marcher.module.lifecycle.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.common.exception.ProductErrorCodeEnum;
import xin.marcher.module.lifecycle.domain.dto.ProductExpriConfigurationDTO;
import xin.marcher.module.lifecycle.domain.request.ProductExpriConfigurationRequest;
import xin.marcher.module.lifecycle.repository.ProductExpriRepository;
import xin.marcher.module.lifecycle.service.ProductExpriConfigurationService;

import java.util.Objects;

/**
 * 考核配置Service
 */
@Service
public class ProductExpriConfigurationServiceImpl implements ProductExpriConfigurationService {

    @Autowired
    private ProductExpriRepository productExpriRepository;

    /**
     * 新增考核配置信息
     *
     * @param request
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveProductExpriConfiguration(ProductExpriConfigurationRequest request) {
        // 参数检查
        checkInsertParams(request);
        return productExpriRepository.saveProductExpriConfiguration(request);
    }

    /**
     * 修改考核配置信息
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateProductExpriConfiguration(ProductExpriConfigurationRequest request) {
        // 参数检查
        checkUpdateParams(request);
        return productExpriRepository.updateProductExpriConfiguration(request);
    }

    /**
     * 查询考核配置信息
     *
     * @param request
     * @return
     */
    @Override
    public ProductExpriConfigurationDTO getProductExpriConfiguration(ProductExpriConfigurationRequest request) {
        return productExpriRepository.getProductExpriConfiguration(request);
    }

    /**
     * 分页查询考核配置信息
     *
     * @param request
     * @return
     */
    @Override
    public PageResult<ProductExpriConfigurationDTO> queryProductExpriConfigurationByPage(ProductExpriConfigurationRequest request) {
        return productExpriRepository.queryProductExpriConfigurationByPage(request);
    }

    /**
     * 新增参数检查
     *
     * @param request
     */
    private void checkInsertParams(ProductExpriConfigurationRequest request) {
        if (Objects.isNull(request) || Objects.isNull(request.getCategoryId())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 更新操作参数检查
     *
     * @param request
     */
    private void checkUpdateParams(ProductExpriConfigurationRequest request) {
        if (Objects.isNull(request) || Objects.isNull(request.getId())) {
            throw new BaseBizException(ProductErrorCodeEnum.PARAM_CHECK_ERROR, ProductErrorCodeEnum.PARAM_CHECK_ERROR.getErrorCode());
        }
    }
}
